package com.api.shopapi.util.smsTencent;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;

import java.util.Random;

public class SendSms {

    //腾讯云，访问管理 ，API密钥
    private static final String secretId = "AKIDqWv3uUnpa5JGvJRIsACXbPxEExPaU6SX";
    private static final String secretKey = "qPhiTw83i5B04rNcbKmxgCSSChwwgBRR";

    //应用列表，应用APPID
    private static final String SmsSdkAppid = "1400566240";




    //签名名称
    private static final String SignName = "银秋科技";
    //模版ID
    private static final String TemplateID = "1102217";


    public String Send(String SendPhone) {
        String TemplateParamSet = null;
        try {
            Credential cred = new Credential(secretId, secretKey);


            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);

            //验证码
            int yzm = random();
            TemplateParamSet = Integer.toString(yzm);

            //模板参数
            //String[] templateParams = {TemplateParamSet,SmsTime};
            String[] templateParams = {TemplateParamSet};

            String[] phoneNumbers = {"+86"+SendPhone};

            SendSmsRequest req = new SendSmsRequest();


            req.setPhoneNumberSet(phoneNumbers);

            req.setSmsSdkAppId(SmsSdkAppid);
            req.setSignName(SignName);
            req.setTemplateId(TemplateID);

            req.setTemplateParamSet(templateParams);

            SendSmsResponse resp = client.SendSms(req);

        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return TemplateParamSet;
    }


    //发送短信
    public String SendSms(String signName,String templateID,String SendPhone,String yzm) {
        String TemplateParamSet = null;
        try {
            Credential cred = new Credential(secretId, secretKey);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);

            //验证码
            //int yzm = random();
            //TemplateParamSet = Integer.toString(yzm);

            //模板参数
            //String[] templateParams = {TemplateParamSet,SmsTime};
            String[] templateParams = {yzm};

            String[] phoneNumbers = {"+86"+SendPhone};

            SendSmsRequest req = new SendSmsRequest();

            req.setPhoneNumberSet(phoneNumbers);

            req.setSmsSdkAppId(SmsSdkAppid);
            req.setSignName(signName);
            req.setTemplateId(templateID);

            req.setTemplateParamSet(templateParams);

            SendSmsResponse resp = client.SendSms(req);

        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return TemplateParamSet;
    }



    /*
    生成6位验证码
    */
    private static int random() {
        Random r = new Random();
        StringBuffer sb = new StringBuffer();
        int[] c = new int[6];
        for (int i = 0; i < 6; i++) {
            c[i] = r.nextInt(9) + 1;
            sb.append(c[i]);
        }
        return Integer.parseInt(sb.toString());

    }


}

